package jiaran.web.Tomxk.old.practice;

import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;

@WebServlet(urlPatterns = "/good/save")
@MultipartConfig(
        location = "D:\\ideaCode\\upload-images",
        maxFileSize = 100 * 1024 *1024,
        maxRequestSize = 100 *1024 *1024*10
)
public class Goods extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String name = req.getParameter("n");
        String price = req.getParameter("p");
        Part image = req.getPart("i");
        String sql = "INSERT INTO goods(`name`,price,`image`) VALUES(?,?,?)";

        String fileName = image.getSubmittedFileName();
        String[] strings = fileName.split("\\.");
        String suffix = strings[1];
        String rename = RandomStringUtils.randomAlphabetic(10) + "." + suffix;
        image.write(rename);

        JdbcUtils.modify(sql,name,price,rename);

        resp.sendRedirect("/Jiaran/user-list.jsp");

    }
}
